<template>
  <div>
    <el-card>
      <div class="header">
        <div>
          <el-input
            v-model="name"
            style="width: 200px; margin-right: 5px"
            placeholder="按销售名称查询"
          ></el-input>
          <el-select v-model="value" placeholder="按照状态查询">
            <el-option
              v-for="item in options"
              :key="item.label"
              :label="item.value"
              :value="item.label"
            >
            </el-option>
          </el-select>
          <el-select v-model="did" placeholder="按照部门查询">
            <el-option
              v-for="item in didList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-select v-model="rolId" placeholder="按照角色查询">
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-button type="primary" @click="handleInit">查询</el-button>
        </div>
        <div>
          <el-button type="primary" @click="handleAdd">添加销售</el-button>
        </div>
      </div>

      <el-table
        :data="tableData"
        style="width: 100%; margin-top: 30px"
        align="center"
      >
        <el-table-column prop="name" label="销售姓名" align="center">
        </el-table-column>

        <el-table-column prop="portrait" label="用户头像">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.portrait !== null"
              style="width: 50px; height: 50px"
              :src="scope.row.portrait"
              fit="contain"
            >
            </el-image>
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column prop="account" label="账号" align="center">
        </el-table-column>
        <el-table-column prop="role" label="角色" align="center">
        </el-table-column>
        <el-table-column prop="did" label="部门" align="center">
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 0">在职</span>
            <span v-else>离职</span>
          </template>
        </el-table-column>

        <el-table-column prop="remark" label="备注" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-dropdown>
              <el-button type="primary">
                操作<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <el-button
                    size="mini"
                    type="primary"
                    @click="handleTask(scope.row.id)"
                  >查看任务
                  </el-button>
                </el-dropdown-item>

                <el-dropdown-item>
                  <el-button
                    size="mini"
                    type="primary"
                    @click="ChangePassword(scope.row.id)"
                  >修改密码
                  </el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button
                    size="mini"
                    type="primary"
                    @click="handleEdit(scope.row.id)"
                  >编辑
                  </el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="Delete(scope.row.id)"
                  >删除
                  </el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="Edit(scope.row.id)"
                  >离职
                  </el-button>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        @current-change="handleCurrentChange"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-card>
    <create ref="create" @Ok="handleInit"></create>
    <edit ref="edit" @Ok="handleInit"></edit>
    <task ref="task" @Ok="handleInit"></task>
    <changePassw ref="changePassw"></changePassw>
  </div>
</template>

<script>
  import {getList} from "@/api/sales";
  import {remove} from "@/api/sales";
  import {EditSales} from "@/api/sales";
  import {list} from "@/api/company";
  import {getRoleList} from "@/api/sales";
  import changePassw from "./components/changePassw";
  import Create from "./components/create";
  import Edit from "./components/edit";
  import Task from "./components/task";


  export default {
    name: "index",
    components: {
      Create,
      changePassw,
      Edit,
      Task,
    },
    data() {
      return {
        tableData: [],
        page: 1,
        size: 10,
        total: 1,
        id: "",
        name: "",
        options: [
          {
            value: "在职",
            label: 0,
          },
          {
            value: "离职",
            label: 1,
          },
        ],
        value: "",
        didList: [],
        did: "",
        rolId: "",
        rolelist: [],
      };
    },
    mounted() {
      this.handleInit();
      this.Didlist();
      this.roleli();
    },
    methods: {
      //销售列表
      handleInit() {
        let form = {
          page: this.page,
          size: this.size,
          name: this.name,
          status: this.value,
          did: this.did,
          role: this.rolId,
        };
        getList(form).then((res) => {
          this.tableData = res.result.records;
          this.total = Number(res.result.total);
        });
      },
      //添加销售
      handleAdd() {
        this.$refs["create"].show();
      },
      handleNext(e) {
        this.page = e;
        this.handleInit();
      },
      // 获取部门列表
      Didlist() {
        let form = {
          page: 1,
          size: 40,
        };
        list(form).then((res) => {
          console.log(res);
          this.didList = res.result.records;
        });
      },
      // 获取角色
      roleli() {
        getRoleList().then((res) => {
          console.log(res);
          this.rolelist = res.result;
        });
      },
      //编辑
      handleEdit(id) {
        this.$refs["edit"].show(id);
      },
      //查看任务
      handleTask(id) {
        this.$refs["task"].show(id);
      },
      //修改密码
      ChangePassword(id) {
        this.$refs["changePassw"].show(id);
      },
      //删除
      Delete(id) {
        this.id = id;
        let form = {
          id: this.id,
        };
        remove(form).then((res) => {
          if (res.status == 200) {
            this.$message.success("删除成功");
            this.handleInit();
          }
        });
      },
      Edit(id) {
        this.id = id;
        let form = {
          id: this.id,
        };
        EditSales(form).then((res) => {
          if (res.status == 200) {
            this.$message.success("操作成功");
            this.handleInit();
          }
        })
      },
      //编辑状态
      swit() {
      },
      //分页
      handleCurrentChange(val) {
        this.page = val;
        this.handleInit();
      },
    },
  };
</script>

<style scoped>
  .header {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
</style>
